﻿function createEntityManager(serviceName) {

    // define the Breeze `DataService` for this app
    var dataService = new breeze.DataService({
        serviceName: serviceName,
        hasServerMetadata: false  // don't ask the server for metadata
    });

    // create the metadataStore
    var metadataStore = new breeze.MetadataStore();

    // initialize it from the application's metadata variable
    metadataStore.importMetadata(window.app.metadata);

    // create a new EntityManager that uses this metadataStore
    return new breeze.EntityManager({
        dataService: dataService,
        metadataStore: metadataStore
    });
}

function createEntity(type) {
    return window['create' + type]();
}

var viewModel = {
    removeItem: function (data) {
        data.entityAspect.setDeleted();
    },
    saveSuccess: function () {
        alert('Save Success');
    },
    saveFail: function () {
        alert("Save failed" + error.entityErrors[0].errorMessage);
    }
};

viewModel.SaveChange = function () {
    if (manager.hasChanges()) {
        manager.saveChanges().then(function () {
        alert('Saved');
        }).fail(function (e) {
            alert(e);
        });
    } else {
        alert('Nothing to save');
    }
}
